import { onBeforeUnmount, onMounted } from "vue";
import { UserAxios } from "@/api";
import { UserStore } from "@/store";
import { PubSubEnum } from "@/types";
import Pubsub from "pubsub-js";

export const UserLogin = () => {
  onMounted(async () => {
    Pubsub.subscribe(PubSubEnum.UPDATE_USERINFO, getUsetInfoDetails);
  });
  const userStore = UserStore();
  //获取用户信息
  const getUsetInfoDetails = () => {
    if (!userStore.isLogin) {
      return false;
    }
    UserAxios.getUserInfo().then(res => {
      userStore.setUserInfoDetails(res);
    });
  };

  onBeforeUnmount(() => {
    Pubsub.unsubscribe(PubSubEnum.UPDATE_USERINFO);
  });
};
